// pages/detailMess/detailMess.js
var choose = require("../../utils/util.js");
Page({
  data: {
    pageInfo: null,
    isBlock: false,
    single: null,
    imgUrl: "",
    title: "",
    price: "",
    unit: "",
    type: "",
    gid: "",
    pageType: ""
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this
    that.setData({
      pageType: options.param
    })
    console.log(options)
    wx.request({
      url: 'http://1.aiym.applinzi.com/aiyouwai.php',
      data: {
        type: "getGoods",
        paras: {
          goodType: options.param
        }
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        that.setData({
          pageInfo: res.data
        })
      }
    })
  },
  // 点击修改页面信息按键弹出弹框 同时加载页面信息
  popover: function (event) {
    var para = this.data.pageInfo[event.currentTarget.dataset.index]
    console.log(para)
    this.setData({
      isBlock: true,
      gid: para ? para.gid : "",
      title: para ? para.title : "",
      imgUrl: para ? para.imgUrl : "",
      type: para ? para.type : "",
      price: para ? para.price : "",
      unit: para ? para.unit : ""
    })
  },
  chooseImg: function () {   // 选择图片并返回路径
    var that = this
    choose.chooseImg(that)
    that.setData({
      single: that.data.imgUrl
    })
  },
  closeBox: function () {  // 关闭弹出盒子
    this.setData({
      isBlock: false
    })
  },
  addNewBlock: function (e) {  // 增加新商品分类弹出商品信息框
    this.popover(e)
    this.setData({
      type: this.data.pageType
    })
  },
  // 添加新信息
  formSubmit: function (e) {
    var obj = this.data.pageInfo;
    console.log(e.detail.value.price)
    var that = this
    var flag = choose.judgePrice(e.detail.value.price);   // 正则验证价格格式
    if(flag){
      wx.request({
      url: 'http://1.aiym.applinzi.com/aiyouwai.php',
      data: {
        type: "updateGoodsInfo",
        paras: e.detail.value
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: { "Content-Type": "application/json" }, // 设置请求的 header
      success: function (res) {
        // success
        console.log(res.data)
        if (!res.data.errcode) {
          wx.showModal({
            content:"添加成功"
          })
          obj.push(res.data.res)
          that.setData({
            isBlock: false,
            pageInfo: obj
          })
        } else {
          wx.showModal({
            title: "提示",
            content: "信息更改/添加失败"
          })
        }
      }
    })
    }
  },
  // 删除商品信息
  deleteTarget: function (e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var obj = this.data.pageInfo;
    wx.showModal({
      title: "警告",
      content: "您将删除一条商品信息！！！",
      success: function () {
        wx.request({
          url: 'http://1.aiym.applinzi.com/aiyouwai.php',
          data: {
            type: "deleteGoods",
            paras: {
              gid: obj[index].gid
            }
          },
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          header: { "Content-Type": "application/json" }, // 设置请求的 header
          success: function (res) {
            // success
            console.log(res)
            if (!res.data.errcode) {
              obj.splice(index, 1)
              that.setData({
                pageInfo: obj
              })
              wx.showModal({
                title: "提示",
                content: "删除成功",
                success: function () {
                }
              })
            }
          }
        })
      }
    })
  }
})